import datetime
import re


def str_date(str):
    # 获取字符串 ”刚刚“
    now_ex = re.compile(r'.*?刚刚.*?')
    # 获取字符串 ”分钟“
    min_ex = re.compile(r'([0-9]*?)分钟')
    # 获取字符串 “小时”
    hour_ex = re.compile(r'([0-9].*?)小时')
    # 获取字符串 “昨天”
    yday_ex = re.compile(r'.*?昨天.*?')
    # 获取字符串 “前天”
    qday_ex = re.compile(r'.*?前天.*?')
    if now_ex.findall(str):
        # 获取本地时间
        date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        return date
    elif min_ex.findall(str):
        # 推算出几分钟前的时间
        min = min_ex.findall(str)[0]
        date = datetime.datetime.now() - datetime.timedelta(minutes=int(min))
        return date.strftime('%Y-%m-%d %H:%M:%S')
    elif hour_ex.findall(str):
        hour = hour_ex.findall(str)[0]
        date = datetime.datetime.now() - datetime.timedelta(hours=int(hour))
        return date.strftime('%Y-%m-%d %H:%M:%S')
    elif yday_ex.findall(str):
        date = datetime.datetime.now() - datetime.timedelta(days=1)
        return date.strftime('%Y-%m-%d %H:%M:%S')
    elif qday_ex.findall(str):
        date = datetime.datetime.now() - datetime.timedelta(days=2)
        return date.strftime('%Y-%m-%d %H:%M:%S')
    else:
        return str
